/* eslint-disable */

import Vue from 'vue'
import 'normalize.css/normalize.css'
import axios from 'axios'
import store from '@/pc/store'
import App from '@/App'
import router from '@/router'
import Db from "@/vendor/db";


import test from "@/vendor/lovefield";
test.doFake();
// import test from "@/vendor/idb";
// test();


Vue.prototype.baseTitle = '小V影视';
Vue.prototype.$VIDEO = $VIDEO;
Vue.prototype.$RECOMMEND = $RECOMMEND;
Vue.prototype.$SLIDE_DATA = $SLIDE_DATA;
Vue.prototype.locked_db = {
  'video': {
    '1': false, '2': false, '3': false, '4': false
  },
  'video_msg': {
    '1': false, '2': false, '3': false, '4': false
  },
  'video_star': {
    '1': false, '2': false, '3': false, '4': false
  }
};
let navbarItems = ['精选'];
for (let i in $VIDEO) {
  let vo = $VIDEO[i];
  navbarItems.push(vo['name']);
}
Vue.prototype.$navbarItems = navbarItems;

Vue.prototype.lock_init=false;
Vue.prototype.initBaseSql = async function(){
  let isLoadDb = sessionStorage.getItem(`is_loaded_db`);
  if (!isLoadDb&&!this.lock_init) {
    this.lock_init=true;
    sessionStorage.clear();
    await new Db().query($BASESQL);
    this.lock_init=false;
    sessionStorage.setItem(`is_loaded_db`, "1");
  }
};

Vue.prototype.initOneDbFile = async (file) => {
  let res = await axios.get(`data/${file}?_=` + new Date().getTime());
  return new Db().query(res.data);
};

//解决并发执行该方法时 插入重复的问题
Vue.prototype.initDb = async function (type, table) {
  await this.initBaseSql();
  let isLoadDb = sessionStorage.getItem(`is_loaded_db_${table}_${type}`);
  let isLockDb=this.locked_db[table][type];
  if (!isLoadDb&&!isLockDb) {
    this.locked_db[table][type]=true;
    let sqlFileList = $VIDEO[type]['db'][table];
    await Promise.all(sqlFileList.map((file) => {
      return this.initOneDbFile(file)
    }))
    sessionStorage.setItem(`is_loaded_db_${table}_${type}`, 1);
    this.locked_db[table][type]=false;
  }
};


Vue.prototype.getVideoByCid = async (cid) => {
  let res = await axios.get(`html/video_${cid}.html`);
  return res.data;
};


Vue.prototype.initBaidu = () => {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?b0f069904c37992c5186a6cba0c0f452";
  var s = document.getElementsByTagName("script")[0];
  s.parentNode.insertBefore(hm, s);
}

Vue.filter('dataFormat', function (value, fmt) {
  let getDate = new Date(value);
  let o = {
    'M+': getDate.getMonth() + 1,
    'd+': getDate.getDate(),
    'h+': getDate.getHours(),
    'm+': getDate.getMinutes(),
    's+': getDate.getSeconds(),
    'q+': Math.floor((getDate.getMonth() + 3) / 3),
    'S': getDate.getMilliseconds()
  };
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (getDate.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  for (let k in o) {
    if (new RegExp('(' + k + ')').test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
    }
  }
  return fmt;
});
Vue.config.productionTip = false


Vue.prototype.init = function () {
  //request默认超时
  axios.defaults.timeout = 5000;

  let last_alert_tt=0;
  let is_alert=false;
  axios.interceptors.response.use(
    response => {
       return response;
    },
    error => {
      let _tt=new Date().getTime()-last_alert_tt;
      if (!is_alert&&_tt>10*1000){
        is_alert=true;
        last_alert_tt=new Date().getTime();
        if(confirm('请检查网络!error：' + error) ){
          sessionStorage.clear();
          window.location.reload();
        }else{
          is_alert=false;
        }
      }
    });


  //检查浏览器是否支持websql
  if (window.openDatabase == undefined) {
    window.location.href = '403.html';
  }
  document.body.removeChild(document.getElementById('Loading'))
  this.checkVersion();
  this.initBaidu();
  if(!/^\#\/view/.test(location.hash)){
    this.initBaseSql();
    //console.log(location.hash);
  }
};

Vue.prototype.checkVersion = async function () {
  let _t = new Date().getTime();
  let res = await axios.get('version.json?_=' + _t);
  let new_version = res.data.version;
  let old_verison = localStorage.getItem('app_version');
  localStorage.setItem('app_version', new_version);
  if (new_version != old_verison) {
    window.location.href = 'index.html?_=' + new_version + window.location.hash;
  }
}
let vue = new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')


vue.init();
